//
//  GRRiceCodeCell.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/9/7.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRRiceCodeCell.h"
#import "GRRiceOrderModel.h"
@implementation GRRiceCodeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(void)updateWithModel:(id)model atIndexPath:(NSIndexPath *)indexPath inView:(UIView *)view{
self.orderModel =(GRRiceOrderModel *) model;
    self.timeLabel.text = self.orderModel.validAtStr;
    self.codeLabel.text = [NSString stringWithFormat:@"验证码%@",self.orderModel.code];
    if ([self.orderModel.state isEqualToString:@"use"] ) {
        [self.codeImageView sd_setImageWithURL:[NSURL URLWithString:self.orderModel.qrCode] placeholderImage:[UIImage imageNamed:@""]];
    }else{
        WEAKSELF
        [self.codeImageView sd_setImageWithURL:[NSURL URLWithString:self.orderModel.qrCode] placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRefreshCached completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            weakSelf.codeImageView.image = [weakSelf grayImage:image ];
        }];
    }
    
    [self.commentBt setTitle:self.orderModel.isComment?@"评价":@"已评价" forState:UIControlStateNormal];
    if ([self.orderModel.state isEqualToString:@"use"] ) {
        self.tipView.hidden = YES;
        self.commentView.hidden = YES;
        self.useView.hidden = NO;
    }else if([self.orderModel.state isEqualToString:@"expired"]||[self.orderModel.state isEqualToString:@"refund"]){//过期
        self.tipView.hidden = NO;
        self.commentView.hidden = YES;
        self.useView.hidden = YES;
        if ([self.orderModel.state isEqualToString:@"expired"]) {
            self.tipLabel.text = @"优惠券未使用且已过有效期，将过期后24小时自动退款";
        }else{
            self.tipLabel.text = @"此优惠券未使用且已过有效期，系统已自动退款";
        }
    }else if([self.orderModel.state isEqualToString:@"complete"]){
        self.tipView.hidden = YES;
        self.commentView.hidden = NO;
        self.useView.hidden = YES;
    }else{
        self.tipView.hidden = YES;
        self.commentView.hidden = YES;
        self.useView.hidden = YES;
    }
    
    
    
    
}

void ProviderReleaseData (void *info, const void *data, size_t size){
    free((void*)data);
}

-(UIImage *)grayImage:(UIImage *)sourceImage

{
    const int imageWidth = sourceImage.size.width;
    const int imageHeight = sourceImage.size.height;
    size_t      bytesPerRow = imageWidth * 4;
    uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
    // create context
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,
                                                 kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
    CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), sourceImage.CGImage);
    // traverse pixe
    int pixelNum = imageWidth * imageHeight;
    uint32_t* pCurPtr = rgbImageBuf;
    for (int i = 0; i < pixelNum; i++, pCurPtr++){
        if ((*pCurPtr & 0xFFFFFF00) < 0x99999900){
            // change color
            uint8_t* ptr = (uint8_t*)pCurPtr;
            ptr[3] = 153; //0~255
            ptr[2] = 153;
            ptr[1] = 153;
        }else{
            uint8_t* ptr = (uint8_t*)pCurPtr;
            ptr[0] = 0;
        }
    }
    
    // context to image
    CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData);
    CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpace,
                                        kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProvider,
                                        NULL, true, kCGRenderingIntentDefault);
    CGDataProviderRelease(dataProvider);
    UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef];
    // release
    CGImageRelease(imageRef);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return resultUIImage;
    
    /*
    int bitmapInfo =kCGImageAlphaNone;
    
    int width = sourceImage.size.width;
    
    int height = sourceImage.size.height;
    
    CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceGray();
    
    CGContextRef context =CGBitmapContextCreate (nil,
                                                 
                                                 width,
                                                 
                                                 height,
                                                 
                                                 8,     // bits per component
                                                 
                                                 0,
                                                 
                                                 colorSpace,
                                                 
                                                 bitmapInfo);
    
    CGColorSpaceRelease(colorSpace);
    
    if (context ==NULL) {
        
        return nil;
        
    }
    
    CGContextDrawImage(context,
                       
                       CGRectMake(0,0, width, height), sourceImage.CGImage);
    
    UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
    
    CGContextRelease(context);
    
    return grayImage;
    */
}



- (IBAction)customerAction:(id)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(detailOrderWithOrderType: orderModel:)]) {
        [_delegate detailOrderWithOrderType:RiceOrderActionTypeCustomerService orderModel:self.orderModel];
    }
}

- (IBAction)returnMoenyAction:(id)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(detailOrderWithOrderType: orderModel:)]) {
        [_delegate detailOrderWithOrderType:RiceOrderActionTypeReturnMoeny orderModel:self.orderModel];
    }
}

- (IBAction)commentAction:(id)sender {
    if (self.orderModel.isComment) {
        if (_delegate && [_delegate respondsToSelector:@selector(detailOrderWithOrderType: orderModel:)]) {
            [_delegate detailOrderWithOrderType:RiceOrderActionTypeComment orderModel:self.orderModel];
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
